package com.ironman.original.generator;


import cn.hutool.core.util.StrUtil;
import com.ironman.event.properties.SysTrainingProperties;
import com.ironman.event.topic.dami.WindTopic;
import lombok.extern.slf4j.Slf4j;
import org.noear.dami.Dami;
import org.noear.snack.ONode;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

@Slf4j
@Component
public class WindOriginalDataGenerator extends OriginalDataGenerator {

    @Resource
    private SysTrainingProperties sysTrainingProperties;

    private String windSourceBoatCode;

    @PostConstruct
    public void init() {
        Dami.<String, Void>bus().listen(WindTopic.GW_WIND_TOPIC, payload -> {
            ONode node = ONode.loadStr(payload.getContent());
            String windSourceBoatCode = node.select("windSourceBoatCode").getString();
            if (!sysTrainingProperties.getFollowTrainingBoatCode().equals(windSourceBoatCode)) {
                return;
            }
            this.windSourceBoatCode = windSourceBoatCode;
        });
    }

    private synchronized String getWind() {
        return StrUtil.format("{}", windSourceBoatCode);
    }

    @Override
    public String dataType() {
        return "wind";
    }

    @Override
    public String generate() {
        return getWind();
    }

    @Override
    public void log(Boolean published, String data) {
        log.info("风速风向: {}\n{}", published, data);
    }
}
